<?php
/*
 * @Author: zhx zhx.0830@qq.com
 * @Date: 2023-08-28 09:27:15
 * @LastEditors: zhx zhx.0830@qq.com
 * @LastEditTime: 2023-09-05 11:35:44
 * @FilePath: \cn-saas-new\app\Http\Requests\RolesRequest.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Request;

/**
 * 平台角色
 *
 * @Desc:验证请求类
 *
 * @author: zhx
 *
 * @Time: 2023/8/10 10:37
 */
class ApplicationRolesRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 获取应用于请求的验证规则。
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required|string|max:255',
            'permissions' => '',
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => '角色名称是必填字段。',
            'permissions.required' => '权限是必选字段。',
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all($keys);
        // 在这里排除 _token 字段
        unset($data['_token']);

        return $data;
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException(response()->json(['msg' => $error[0], 'code' => '500', 'data' => 'error'], 500));
    }
}
